# 河南省内数据
import json
from pyecharts.charts import Map
from pyecharts.options import *

def get_city_data(file_name,province_name):
    with open(file_name, 'r', encoding='utf-8') as f:
        file_str = f.read()
        file_json = json.loads(file_str)
        province_data_list = file_json['areaTree'][0]['children']
        city_data_list=None
        for province_data in province_data_list:
            if province_data['name'] == province_name:
                city_data_list = province_data['children']
                break
        if city_data_list is None:
            raise Exception(f'没有找到对应的省份数据：{province_name}')

        result = []
        for item in city_data_list:
            city_name = item['name']+'市'
            city_confirm = item['total']['confirm']
            result.append((city_name, city_confirm))
        return result


if __name__ == '__main__':
    province_name='陕西'
    city_data=get_city_data('../static/疫情.txt',province_name)
    print(city_data)

    map=Map()
    map.add(province_name+'疫情分布图',city_data,maptype=province_name)
    map.set_global_opts(
        title_opts=TitleOpts(title=province_name+'疫情分布图'),
        visualmap_opts=VisualMapOpts(
            is_show=True,
            is_piecewise=True,
            pieces=[
                {"min": 1, "max": 9, "label": "1-9人", "color": "#CCFFFF"},
                {"min": 10, "max": 99, "label": "10-99人", "color": "#FFFF99"},
                {"min": 100, "max": 499, "label": "99-499人", "color": "#FF9966"},
                {"min": 500, "max": 999, "label": "499-999人", "color": "#FF6666"},
                {"min": 1000, "max": 9999, "label": "1000-9999人", "color": "#CC3333"},
                {"min": 10000, "label": "10000以上", "color": "#990033"}
            ]
    ))
    map.render('../output/'+province_name+'疫情分布图.html')
    print('生成完毕')